Array.prototype.max = function(){ 
    return Math.max.apply({},this); 
};
Array.prototype.min = function(){ 
    return Math.min.apply({},this); 
};

//获取当前时间
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
if (month < 10) {
    month = "0" + month;
}
if (day < 10) {
    day = "0" + day;
}
var nowDate = year + "-" + month + "-" + day;

// 选择路灯
function showData(lightId) {
	$("#lightId").val(lightId);
	var lightId = $("#lightId").val();
	if(lightId=="") {
		alert("请选择对应的路灯");
		return;
	}
	
	$("#form1").submit();
}

$(document).ready(function(){
	// .home-aside-p1:hover {z-index: 3}
    $(".home-aside-p1 .aside-li").mouseenter(function(){
        // $(".home-aside-p1").css({'z-index':3})
        $(this).addClass("active").siblings().removeClass("active");
        var topPx1 = $(this).offset().top;
        var topPx2 = $(this).position().top;
        var contHtml = $(this).find(".li-cont").html();
        var bottomPx = $(window).height()-topPx1-50;
        //console.log(topPx1, topPx2,bottomPx);
        var cssObj = {};
        if(topPx1 > 220) {
            cssObj = {bottom:bottomPx,top:'unset'};
        } else {
            cssObj = {top:topPx1,bottom:'unset'};
        }
        $(".aside-move-panel").html(contHtml).css(cssObj).show();
    });

    $(".aside-move-panel").mouseleave(function(){
        $(this).hide();
        $(".home-aside-p1 .aside-li").removeClass("active");
    });
	
	// 获取实时数据
	queryLightLatestStatus();

    /*WdatePicker({
        eCont:'myDate'
        // vel:'dateInput'
        // ,
        // onpicked:function(dp){alert('你选择的日期是:'+dp.cal.getDateStr())}
    });*/

    // 基于准备好的dom，初始化echarts实例
    var leftChartCont = echarts.init(document.getElementById('leftChartCont'));
    
    var colors = ['#ffeca2', '#fcb8b7', '#1195f0'];
    var leftChartOption = {
            color: colors,
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross'
                }
            },
            grid: {
                right: '20%'
            },
            // toolbox: {
            //     feature: {
            //         dataView: {show: true, readOnly: false},
            //         restore: {show: true},
            //         saveAsImage: {show: true}
            //     }
            // },
            legend: {
                data:['放电','充电','电量']
            },
            xAxis: [
                {
                    type: 'category',
                    axisTick: {
                        alignWithLabel: true
                    },
                    data: []
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    name: '放电',
                    min: 0,
                    max: 250,
                    position: 'left',
                    axisLine: {
                        lineStyle: {
                            color: '#666'
                        }
                    },
                    axisLabel: {
                        formatter: '{value} AH'
                    }
                },
                // {
                //     type: 'value',
                //     name: '充电',
                //     min: 0,
                //     max: 250,
                //     position: 'right',
                //     offset: 80,
                //     axisLine: {
                //         lineStyle: {
                //             color: colors[1]
                //         }
                //     },
                //     axisLabel: {
                //         formatter: '{value} ml'
                //     }
                // },
                {
                    type: 'value',
                    name: '电量',
                    min: 0,
                    max: 100,
                    position: 'right',
                    axisLine: {
                        lineStyle: {
                            color: colors[2]
                        }
                    },
                    axisLabel: {
                        formatter: '{value} %'
                    }
                }
            ],
            series: [
                {
                    name:'放电',
                    type:'bar',
                    data:[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
                },
                {
                    name:'充电',
                    type:'bar',
                    // yAxisIndex: 1,
                    data:[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
                },
                {
                    name:'电量',
                    type:'line',
                    smooth:true,
                    yAxisIndex: 1,
                    data:[2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
                }
            ]
        };

    // 创建随机数假数据
    var createRandomFun = function(min,max){
        var num2 = Math.floor(Math.random()*(max-min));
        return min+num2;
    };

    // 使用刚指定的配置项和数据显示图表。
    initLeftChartFun();

    $("#dateSelect").change(function(){
    	$("#form1").submit();
    });

    function initLeftChartFun(){
        var val = $("#dateSelect").val();

        leftChartOption.xAxis[0].data = dateX;
        
        leftChartOption.series[0].data = Ydata1;
        leftChartOption.series[1].data = Ydata2;
        leftChartOption.series[2].data = Ydata3;

        var max1Leeter = getMaxShowNumFun(Ydata1);
        var max2Leeter = getMaxShowNumFun(Ydata2);
        
        if(max1Leeter>max2Leeter) {
        	leftChartOption.yAxis[0].max = max1Leeter*1;
        }else {
        	leftChartOption.yAxis[0].max = max2Leeter*1;
        }
        
        leftChartCont.setOption(leftChartOption);
    }

    // 获取整位最大数
    function getMaxShowNumFun(arr){
        var max1 =  arr.max()+'';
        var max1Leeter1 = (max1.substr(0,1)*1)+1;
        max1Leeter1 = max1Leeter1 + '';
        var max1LeeterZ = '';
        for(var i=0; i<max1.length-1; i++) {
            max1LeeterZ+='0';
        }
        return max1Leeter = max1Leeter1+max1LeeterZ;
    }

    // 图表右侧
    var rightChartCont = echarts.init(document.getElementById('rightChartDiv'));

    var rightChartOption = {
        tooltip : {
            trigger: 'axis'
        },
        calculable : true,
        xAxis : [
            {
                type : 'category',
                boundaryGap : false,
                data : tabx
            }
        ],
        yAxis : [
            {
                type : 'value'
            }
        ],
        series : []
    };

    function initRightChartFun(){
        var activeTab = $("#rightChartNav .active").attr('data-id');
        // 根据id
        if(activeTab == 1) {
            
            var tabSeries1 = [
                {
                    name:'充电电流',
                    type:'line',
                    smooth:true,
                    itemStyle: {normal: {color:"#c490bf", areaStyle: {
                        // color:"#e2acda",
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#c490bf'
                        }, {
                            offset: 1,
                            color: '#ffffff'
                        }]),
                        type: 'default'
                    }}},
                    data:[]
                },
                {
                    name:'放电电流',
                    type:'line',
                    smooth:true,
                    itemStyle: {normal: {color:"#008def", areaStyle: {
                        // color:"#e6f1b2",
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#008def'
                        }, {
                            offset: 1,
                            color: '#ffffff'
                        }]),
                        type: 'default'}}},
                    data:[]
                }
            ];

            tabSeries1[0].data = tab1data1;
            tabSeries1[1].data = tab1data2;

            rightChartOption.series = tabSeries1;

            rightChartCont.setOption(rightChartOption);
        } else if(activeTab == 2){
            var tabSeries2 = [
                {
                    name:'电池电压',
                    type:'line',
                    smooth:true,
                    itemStyle: {normal: {color:"#ef9c3b", areaStyle: {
                        // color:"#e2acda",
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#ef9c3b'
                        }, {
                            offset: 1,
                            color: '#ffffff'
                        }]),
                        type: 'default'}}},
                    data:[]
                }
            ];

            tabSeries2[0].data = tab2data1;
            rightChartCont.clear();
            rightChartOption.series = tabSeries2;

            rightChartCont.setOption(rightChartOption);
        } else if(activeTab == 3){
            var tabSeries3 = [
                {
                    name:'电池温度',
                    type:'line',
                    smooth:true,
                    itemStyle: {normal: {color:"#2dcec3", areaStyle: {
                        // color:"#e2acda",
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#2dcec3'
                        }, {
                            offset: 1,
                            color: '#ffffff'
                        }]),
                        type: 'default'}}},
                    data:[]
                }
            ];

            tabSeries3[0].data = tab3data1;
            rightChartCont.clear();
            rightChartOption.series = tabSeries3;

            rightChartCont.setOption(rightChartOption);
        } else if(activeTab == 4){
            var tabSeries4 = [
                {
                    name:'太阳板电压',
                    type:'line',
                    smooth:true,
                    itemStyle: {normal: {color:"#f3f093", areaStyle: {
                        // color:"#e2acda",
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#f3f093'
                        }, {
                            offset: 1,
                            color: '#ffffff'
                        }]),
                        type: 'default'}}},
                    data:[]
                }
            ];

            tabSeries4[0].data = tab4data1;
            rightChartCont.clear();
            rightChartOption.series = tabSeries4;

            rightChartCont.setOption(rightChartOption);
        }
    }

    initRightChartFun();

    $("#rightChartNav .tab-link").click(function(){
        $(this).addClass('active').siblings().removeClass('active');
        initRightChartFun();
    });

});